//Start: 2024-12-26 10:26:29
#include<bits/stdc++.h>
#define For(i,j,k) for(int i=(j);i<=(k);++i)
#define ForDown(i,j,k) for(int i=(j);i>=(k);--i)
#define Debug(fmt, args...) fprintf(stderr,"(func %s, line #%d): " fmt, __func__, __LINE__, ##args),fflush(stderr)
#define debug(fmt, args...) fprintf(stderr,fmt,##args),fflush(stderr)
#define within :
#define LJY main
using namespace std;
typedef long long ll;
const int N=2e5+5;
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
inline int read(){
  char ch=getchar();int x=0,f=1;
  while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
  while(ch>='0'&&ch<='9')
    x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
  return x*f;
}
int n,q,c,a[N];
struct Block{
  int c[1<<9][1<<6];
  void update(int x,int v){
    int p=x>>6,q=x&63;
    For(i,q,63) if((i&q)==q) c[p][i]+=v;
  }int query(int x){
    int p=x>>6,q=x&63,ans=0;
    For(i,0,p) if((i&p)==i) ans+=c[i][q];
    return ans;
  }
}blc;
int f[N],dep[N],fa[N][18],st[N][18];
vector<int>G[N];
vector<pair<int,int> >mdf[N],qry[N];int ans[N];
void dfs(int u){
  fa[u][0]=f[u];st[u][0]=1<<a[u];
  For(i,1,17)
    fa[u][i]=fa[fa[u][i-1]][i-1],
    st[u][i]=st[fa[u][i-1]][i-1]|st[u][i-1];
  for(int v within G[u]) dep[v]=dep[u]+1,dfs(v);
}
void solve(int u){
  for(auto [S,id] within qry[u]) ans[id]-=blc.query(S);
  for(auto [x,v] within mdf[u]) blc.update(x,v);
  for(int x within G[u]) solve(x);
  for(auto [S,id] within qry[u]) ans[id]+=blc.query(S);
}
signed LJY(){
  freopen("tree.in","r",stdin);
  freopen("tree.out","w",stdout);
  n=read();q=read();c=read();
  For(i,1,n) a[i]=read();
  For(i,2,n) G[f[i]=read()].emplace_back(i);
  dep[1]=1;dfs(1);
  For(i,1,n){
    int now=1<<a[i],p=i;
    mdf[i].emplace_back(now,1);
    For(i,1,c-1){
      ForDown(i,__lg(dep[p]),0)
        if((st[p][i]|now)==now) p=fa[p][i];
      if(!p) break;
      mdf[p].emplace_back(now,-1);
      now|=1<<a[p];
      mdf[p].emplace_back(now,1);
    }
  }For(i,1,q){
    int S=read(),u=read(),p=u;
    if(!(S&(1<<a[u]))){ans[i]=1;continue;}
    ForDown(i,__lg(dep[u]),0)
      if((st[f[p]][i]&S)==st[f[p]][i]) p=fa[p][i];
    if(!p) p=1;
    qry[p].emplace_back(S,i);
  }solve(1);
  For(i,1,q) printf("%d\n",ans[i]);
  return 0;
}